package com.zkkj.demo.handle;

import com.zkkj.demo.exception.ForbiddenException;
import com.zkkj.demo.exception.SysException;
import com.zkkj.demo.exception.UnauthorizedException;
import com.zkkj.demo.exception.ValidateFailedException;
import com.zkkj.demo.vo.base.AResultVo;
import com.zkkj.demo.vo.base.BaseResultVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


/**
 * author : cyc
 * Date : 2019/12/26
 */

@RestControllerAdvice
public class GlobalExceptionHandle {

    private Logger logger = LoggerFactory.getLogger(GlobalExceptionHandle.class);

    @ExceptionHandler(value = Exception.class)
    public AResultVo resultFail(Exception e) {
        AResultVo aResultVo = new BaseResultVo();
        logger.error("GlobalExceptionHandle.resultFail", e);
        if (e instanceof ValidateFailedException) {
            return ((BaseResultVo) aResultVo).validateFailed(null,e.getMessage());
        } else if (e instanceof UnauthorizedException) {
            return ((BaseResultVo) aResultVo).unauthorized(null);
        } else if (e instanceof ForbiddenException) {
            return ((BaseResultVo) aResultVo).forbidden(null);
        } else if (e instanceof SysException) {
            return ((BaseResultVo) aResultVo).failure(null,e.getMessage());
        }
        return aResultVo.failure(null, "系统异常");
    }
}
